package com.lqz.tools;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

//import com.lqz.formBean.MatForm;

public class CustomClass {
	 public static <T> List<T>  rs2Bean(ResultSet rs, T object) throws Exception {
	        Class<?> classType = object.getClass();
	        ArrayList<T> objList = new ArrayList<T>();
	        //SqlRowSet srs = jdbcTemplate.queryForRowSet(sql);
	        Field[] fields = classType.getDeclaredFields();//得到对象中的字段
	        while (rs.next()) {
	        	
	            //每次循环时，重新实例化一个与传过来的对象类型一样的对象
	            @SuppressWarnings("unchecked")
				T objectCopy = (T) classType.getConstructor(new Class[] {}).newInstance(new Object[] {});
	            for (int i = 0; i < fields.length; i++) {
	                Field field = fields[i];
	                String fieldName = field.getName();
	                Object value = null;
	                //根据字段类型决定结果集中使用哪种get方法从数据中取到数据
	                if (field.getType().equals(String.class)) {
	                    value = rs.getString(fieldName);
	                    if(value==null){
	                        value="";
	                    }
//	                    System.out.println(value);
	                }else  if (field.getType().equals(int.class)) {
	                    value = rs.getInt(fieldName);
	                }else  if (field.getType().equals(java.util.Date.class)) {
	                    value = rs.getDate(fieldName);
	                }else if(field.getType().equals(float.class)){
	                	value = rs.getFloat(fieldName);
	                }else if(field.getType().equals(boolean.class)){
	                	value = rs.getBoolean(fieldName);
	                }
	                // 获得属性的首字母并转换为大写，与setXXX对应
	                String firstLetter = fieldName.substring(0, 1).toUpperCase();
	                String setMethodName= "set" + firstLetter + fieldName.substring(1);
	                Method setMethod = classType.getMethod(setMethodName,
	                        new Class[] { field.getType() });
	                setMethod.invoke(objectCopy, new Object[] { value });//调用对象的setXXX方法
	            }
	            objList.add(objectCopy);
	        }
	        return objList;
	    }
	 public static <T> List<T>  rs2BeanNA(ResultSet rs, T object) throws Exception {
		 Class<?> classType = object.getClass();
		 ArrayList<T> objList = new ArrayList<T>();
		 //SqlRowSet srs = jdbcTemplate.queryForRowSet(sql);
		 Field[] fields = classType.getDeclaredFields();//得到对象中的字段
		 while (rs.next()) {
			 
			 //每次循环时，重新实例化一个与传过来的对象类型一样的对象
			 @SuppressWarnings("unchecked")
			 T objectCopy = (T) classType.getConstructor(new Class[] {}).newInstance(new Object[] {});
			 for (int i = 0; i < fields.length; i++) {
				 Field field = fields[i];
				 String fieldName = field.getName();
				 Object value = null;
				 //根据字段类型决定结果集中使用哪种get方法从数据中取到数据
				 if (field.getType().equals(String.class)) {
					 value = rs.getString(fieldName);
					 if(value==null){
						 value="";
					 }
//	                    System.out.println(value);
				 }else  if (field.getType().equals(int.class)) {
					 value = rs.getInt(fieldName);
				 }else  if (field.getType().equals(java.util.Date.class)) {
					 value = rs.getDate(fieldName);
				 }else if(field.getType().equals(float.class)){
					 value = rs.getFloat(fieldName);
				 }else if(field.getType().equals(boolean.class)){
					 value = rs.getBoolean(fieldName);
				 }
				 if(value.equals("")){
					 value = "N/A";
				 }
				 // 获得属性的首字母并转换为大写，与setXXX对应
				 String firstLetter = fieldName.substring(0, 1).toUpperCase();
				 String setMethodName= "set" + firstLetter + fieldName.substring(1);
				 Method setMethod = classType.getMethod(setMethodName,
						 new Class[] {  Class.forName("java.lang.String") });
				 setMethod.invoke(objectCopy, new Object[] { value });//调用对象的setXXX方法
			 }
			 objList.add(objectCopy);
		 }
		 return objList;
	 }
	 //将传进来的javabean转为一个二维数组，数组第一个值为列名，第二个值为列名对应的值。
	 public static <T> String[] bean2FV(T object){
		 String[] res=new String[2];
		 Class<?> classType = object.getClass();
		 Field[] fields = classType.getDeclaredFields();//得到对象中的字段
	     String fieldsString="";  
	     String valuesString="";  
		 for(Field field :fields){
	           String fieldName = field.getName();
	           fieldsString = fieldsString+fieldName+",";
	           String firstLetter = fieldName.substring(0, 1).toUpperCase();
               String getMethodName= "get" + firstLetter + fieldName.substring(1);
               try {
				Method getMethod = classType.getMethod(getMethodName,
				           new Class[] { field.getType() });
				try {
					valuesString = valuesString+getMethod.invoke(classType)+",";
				} catch (IllegalAccessException | IllegalArgumentException
						| InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}//调用对象的getXXX方法
			} catch (NoSuchMethodException | SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
               
	       }
		 res[0]=fieldsString;
		 res[1]=valuesString;
		 return res;
	 }
	 public static <T> List<T>  rs2Bean1(ResultSet rs, T object) throws Exception {
	        Class<?> classType = object.getClass();
	        ArrayList<T> objList = new ArrayList<T>();
	        //SqlRowSet srs = jdbcTemplate.queryForRowSet(sql);
	        Field[] fields = classType.getDeclaredFields();//得到对象中的字段
	        ResultSetMetaData rsmd=rs.getMetaData();
	        int size=rsmd.getColumnCount();
	        while (rs.next()) {
	        	
	            //每次循环时，重新实例化一个与传过来的对象类型一样的对象
	            @SuppressWarnings("unchecked")
				T objectCopy = (T) classType.getConstructor(new Class[] {}).newInstance(new Object[] {});
	            for (int i = 1; i <= size; i++) {
	                Field field = fields[i];
	                String fieldName = field.getName();
	                Object value = null;
	                value=rs.getString(i);
	                // 获得属性的首字母并转换为大写，与setXXX对应
	                String firstLetter = fieldName.substring(0, 1).toUpperCase();
	                String setMethodName= "set" + firstLetter + rsmd.getColumnName(i).substring(1);
	                Method setMethod = classType.getMethod(setMethodName,
	                        new Class[] { field.getType() });
	                setMethod.invoke(objectCopy, new Object[] { value });//调用对象的setXXX方法
	            }
	            objList.add(objectCopy);
	        }
	        return objList;
	    }
}
